;
(function() {
  new Vue({
    el: '#matter',
    data: {
      flag: true,
      modulesList: [],
      keyword: '',
      publishingType: '#',
      moduleCode: [],
      data: [],
      dataItem: '', //编辑的单个数据
      quanxian: JSON.parse(window.localStorage.getItem('permissions')),
      isEdit: false,
      isEditobj: '',
      templateId: '',
    },
    methods: {
      //获取素材类型
      getmodules() {
        hlhttp.get({
          url: 'configure/dict',
          data: {
            module_code: 'MNHU',
            config_name: 'MNHU_PAGE_MATERIAL_TYPE',
            parent_dict_code: '#',
            key: '#',
          },
          success: (data) => {
            this.modulesList = hlArrayUtil.replaceData(data, [{key: 'dictName', rKey: 'name'}, {key: 'dictCode', rKey: 'code'}]);
          }
        })
      },
      //选择素材类型
      eventModule(arr) {
        this.moduleCode = [arr[0].code]
      },

      //重置
      reset() {
        this.keyword = '';
        this.moduleCode = [];
        this.getTemplateBlocks(this.templateId);
      },
      //查询
      search() {
        hlhttp.get({
          url: 'portal/_search_blocks',
          data: {
            'template_id': this.templateId,
            'keyword': this.keyword || '#',
            'block_type': this.moduleCode.length ? this.moduleCode[0] : '#'
          },
          success: (data) => {
            this.data = data;
          }
        })
      },
      //租户模版获取
      getTemplateId() {
        hlhttp.get({
          url: 'portal/template',
          success: (data) => {
            this.getTemplateBlocks(data.templateId)
            this.templateId = data.templateId
          }
        })
      },
      // 区块信息列表获取
      getTemplateBlocks(id) {
        this.data = []
        hlhttp.get({
            url: 'portal/template_blocks',
            data: {
              template_id: id,
              block_type: 'banner',
            },
            success: (data) => {
              data.forEach(item => {
                if (!'memo' in item) {
                  item.memo = ''
                }
                this.data.push(item)
              })
            }
          }),
          hlhttp.get({
            url: 'portal/template_blocks',
            data: {
              template_id: id,
              block_type: 'video',
            },
            success: (data) => {
              data.forEach(item => {
                if (!'memo' in item) {
                  item.memo = ''
                }
                this.data.push(item)
              })
            }
          })
      },
      // 编辑
      edit(obj) {
        this.isEdit = true
        this.isEditobj = JSON.parse(JSON.stringify(obj))
      },
      // 确定修改区块信息
      sureBlockMeg() {
        hlhttp.put({
          url: 'portal/block',
          data: {
            'template_id': this.templateId,
            'block_id': this.isEditobj.blockId,
            'block_title': this.isEditobj.blockTitle,
            'memo': this.isEditobj.memo || '#',
            'seq': '#'
          },
          success: (data) => {
            hl.toast('编辑成功')
            this.isEdit = false
            this.getTemplateBlocks(this.templateId)
          }
        })
      },
      //去管理页面
      gotoRegulate(item) {
        let type = ''
        if (item.blockType == 'banner') {
          type = 'img'
        }
        if (item.blockType == 'video') {
          type = 'video'
        }
        hlUrlUtil.goTo('matterRegulate_detail', {
          templateId: this.templateId,
          block_id: item.blockId,
          type: type,
          ddtab: true
        }, true)
      },

    },
    created() {
      hlSelectUi()
    },
    mounted() {
      this.getmodules()
      this.getTemplateId()
    }
  })
}())
